const root = Symbol("root");

export class Component {
  [root];
  constructor(options, store, isNew) {
    let _this = this
    for (let name in options) {
      // eslint-disable-next-line no-prototype-builtins
      if (options.hasOwnProperty(name)) {
        this[name] = options[name];
      }
    }

    let key = Date.parse(new Date()) + "_" + Math.ceil(Math.random() * 99999);
    if (!this.key || isNew) {
      this.key = key;
      this.cid = "cid_" + key;
    }

    if (this.model&&!this.model.startsWith('model_')) {

      let arr = this.model.split('.')
      var end = arr.pop()

      let map = store.models

      try {
        while (arr.length > 0) {
          let keyName = arr.shift()
          map = map[keyName]
        }
  
        _this['options']['defaultValue'] = map[end]
      } catch (error) {
          console.log("🚀 ~ file: component.js ~ line 37 ~ Component ~ constructor ~ error", error)
      }


      Object.defineProperty(_this['options'], 'defaultValue', {
        get() {
          return map[end];
        },
        set(value) {
          map[end] = value;
        },
      });

    }

    this[root] = store;
  }
  getOptions(key) {
    return this["options"][key];
  }
  setOptions(key, value) {
    this["options"][key] = value;
  }
  getRoot() {
    return this[root];
  }
}
